//导包
importClass(android.graphics.PorterDuffXfermode) //导入安卓PorterDuff画笔设置类

//变量创建
//定义悬浮窗、画布
var win = floaty.rawWindow(
    <frame gravity="center">
        <canvas id="board" w="auto" h="auto" />
    </frame>
);
//创建位置列表
var position = []
for (n = 0; n < 5; n++) {
    if (n == 0) {
        position.push([Math.floor(Math.random() * device.width), Math.floor(Math.random() * (device.height / 7 - 3 + 1) + 3)])
    } else {
        while (true) {
            x = Math.floor(Math.random() * device.width);
            y = Math.floor(Math.random() * (device.height / 7 - 3 + 1) + 3);
            if (position.indexOf([x, y]) > -1) {
                continue;
            } else {
                position.push([x, y])
                break;
            }
        }
    }
}
//定义画笔
var paint = new Paint();
//设置画笔和悬浮窗
win.setTouchable(false); //悬浮窗不可触摸
win.setSize(-1, -1); //-1为铺满屏幕
var img = images.read("./res/藤原千花.png") //读图
var b = images.toBytes(img); //将图片转换为字节数组
var bm = android.graphics.BitmapFactory.decodeByteArray(b, 0, b.length); //字节数组转bitmat
x = 0;
y = 0;
//画布监听
win.board.on("draw", (canvas) => {
    //橡皮清空画布
    xfermode = new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR); //画笔路径path为橡皮模式，mode为clear，橡皮是一种特殊的画笔
    paint.setXfermode(xfermode);
    canvas.drawPaint(paint);
    paint.setXfermode(null); //清空路径和画布
    canvas.drawColor(colors.parseColor("#00000000"));
    for (n = 0; n < position.length; n++) {
        x = position[n][0];
        y = position[n][1];
        if (x == undefined) {
            x = 0;
        }
        if (y == undefined) {
            y = 0;
        }
        canvas.drawBitmap(bm, x, y, paint);
    }
})
//创建定时器，每17ms触发一次间隔
threads.start(function() {
    setInterval(() => {
        position = position.map(n => n.map(p => p >= device.width ? p = 0 : p += 1));
    }, 17);
})